package adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.liugl.recyclerviewtest.R

///布局类型
enum class LayoutType { SIMPLE, HORIZONTAL, GRID, WATERFALL }

class FruitAdapter(private val fruitList: ArrayList<Fruit>, private val type: LayoutType) :
    RecyclerView.Adapter<FruitAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
        val fruitName: TextView = view.findViewById(R.id.fruitName)
        val outer: LinearLayout = view.findViewById(R.id.outerLayout)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutId: Int = when (type) {
            LayoutType.SIMPLE -> R.layout.fruit_item
            LayoutType.HORIZONTAL -> R.layout.hor_fruit_item
            LayoutType.GRID -> R.layout.hor_fruit_item
            LayoutType.WATERFALL -> R.layout.hor_fruit_item
        }

        val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = fruitList[position]
        holder.fruitImage.setImageResource(fruit.fruitImage)
        holder.fruitName.text = fruit.fruitName
        holder.fruitName.paint.isFakeBoldText = true

        if (type == LayoutType.GRID) holder.outer.layoutParams.width =
            WindowManager.LayoutParams.MATCH_PARENT
    }

    override fun getItemCount(): Int = fruitList.size
}